package com.zhuojy.equity.mall.admin.web.controller;

import com.zhuojy.equity.mall.admin.common.entity.TRole;
import com.zhuojy.equity.mall.admin.common.exception.ExceptionUtil;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.common.support.Result;
import com.zhuojy.equity.mall.admin.common.utils.LogUtil;
import com.zhuojy.equity.mall.admin.service.RoleService;
import com.zhuojy.equity.mall.admin.service.dto.req.CreateRoleReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.DeleteRoleReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.ModifyRoleReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.CreateRoleResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.DeleteRoleResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.ModifyRoleResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.QueryAllRolesResDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.*;

/**
 * 后台用户角色管理Controller
 */
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@Api(tags = "角色模块",value = "角色模块" )
public class RoleController {

	static Logger log = LoggerFactory.getLogger(RoleController.class);

	private final RoleService roleService;

	/**
	 * 添加角色
	 */
	@PostMapping(value = "/createRole")
	@ApiOperation(value = "添加角色",httpMethod = "POST",notes = "添加角色")
	public Result<CreateRoleResDTO> createRole(@RequestBody CreateRoleReqDTO createRoleReqDTO) {
		String methodName = "添加角色";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, createRoleReqDTO);
		Result<CreateRoleResDTO> result;
		try {
			result = roleService.createRole(createRoleReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 修改角色
	 */
	@PostMapping(value = "/modifyRole")
	public Result<ModifyRoleResDTO> modifyRole(@RequestBody ModifyRoleReqDTO modifyRoleReqDTO) {
		String methodName = "修改角色";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, modifyRoleReqDTO);
		Result<ModifyRoleResDTO> result;
		try {
			result = roleService.modifyRole(modifyRoleReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 删除角色
	 */
	@PostMapping(value = "/deleteRole")
	public Result<DeleteRoleResDTO> deleteRole(@RequestBody DeleteRoleReqDTO deleteRoleReqDTO) {
		String methodName = "删除角色";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, deleteRoleReqDTO);
		Result<DeleteRoleResDTO> result;
		try {
			result = roleService.deleteRole(deleteRoleReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 查询所有角色
	 */
	@PostMapping(value = "/queryAllRoles")
	public ResultEntity queryAllRoles(@RequestBody TRole tRole, @RequestParam(value = "pageSize") Integer pageSize,@RequestParam("pageNum") Integer pageNum) {

		return roleService.queryAllRoles(tRole,pageSize,pageNum);
	}
}
